package com.laozhao.deliveryManager.test.sqlmap;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DBUtil {

	public static List rs2list(ResultSet rs) throws Exception {
		List list = new ArrayList();
		if (rs == null) {
			return list;
		}
		String[] arr = null;
		Map map = null;
		arr = getColName(rs);
		int i = 0;
		int num = 0;
		String v = null;
		if (arr != null) {
			num = arr.length;
			while (rs.next()) {
				map = new HashMap();
				for (i = 1; i <= num; i++) {
					v = rs.getString(i);
					if (v == null) {
						v = "";
					}
					map.put(arr[i - 1], v);
				}// end for
				list.add(map);
			}// end while
		}// end if
		return list;
	}

	// 支持分页，基于结果集的通用"假"分页
	// 把结果集全部装载到内存，（数据量不大时完全可以这么做）
	// 对于大数据量查询，请设置statementset的setMaxRows参数，限制resultSet大小即可
	// 要不然会很慢且吃内存

	public static List rs2list(ResultSet rs, int skip, int page_size)
			throws Exception {
		List list = new ArrayList();

		if (rs == null) {
			return list;
		}
		String[] arrColName = null;
		String colName = null;
		int colNum = 0;
		int pos = 0;
		int i = 0;
		int j = 0;
		Map map = null;
		String v = null;

		ResultSetMetaData rsmd = null;
		rsmd = rs.getMetaData();
		colNum = rsmd.getColumnCount();
		arrColName = new String[colNum];

		for (i = 1; i <= colNum; i++) {
			arrColName[i - 1] = rsmd.getColumnName(i).toLowerCase();
		}

		if (page_size == 0) {
			page_size = 10000000;
		}

		while ((rs.next()) && (j < page_size)) {

			if (pos < skip) {
				pos++;
				continue;
			}

			map = new HashMap();

			for (i = 0; i < colNum; i++) {
				v = rs.getString(i + 1);
				if (v == null) {
					v = "";
				}
				map.put(arrColName[i], v);
			}

			j++;
			list.add(map);
		}// end while

		return list;

	}

	public static String[] getColName(ResultSet rs) throws Exception {

		if (rs == null) {
			return null;
		}
		int num = 0;
		int i = 0;
		String colName = null;
		String[] arr = null;
		ResultSetMetaData rsmd = null;
		rsmd = rs.getMetaData();
		num = rsmd.getColumnCount();
		arr = new String[num];
		for (i = 1; i <= num; i++) {
			colName = rsmd.getColumnName(i);
			colName = colName.toLowerCase();
			arr[i - 1] = colName;
		}
		return arr;
	}

	public static ResultSet executeQuery(Statement stmt, String sql) {
		// TODO Auto-generated method stub
		return null;
	}

	public static void close(ResultSet rs) {
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static void close(Statement stmt) {
		try {
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static Connection getConn() {
		// TODO Auto-generated method stub
		return null;
	}

	public static void close(Connection cn) {
		try {
			cn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static int executeUpdate(Statement stmt, String sql) {
		// TODO Auto-generated method stub
		return 0;
	}
}
